<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="关键字">
              <el-input v-model="query.blurry" placeholder="请输入关键字" maxlength="255" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleAdd()">新增</el-button>
              <el-button type="primary" @click="handleQuery('')">查询</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            ref="table"
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 300px)"
            lazy
            row-key="id"
            :load="getMenus"
            :data="tableData"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            @selection-change="selectionChange"
          >
            <el-table-column type="selection" width="45" />
            <el-table-column prop="title" label="菜单标题" show-overflow-tooltip />
            <el-table-column :show-overflow-tooltip="true" prop="permission" label="权限标识" />
            <el-table-column :show-overflow-tooltip="true" prop="component" label="组件路径" />
            <el-table-column prop="menuSort" label="排序">
              <template slot-scope="scope">
                {{ scope.row.menuSort }}
              </template>
            </el-table-column>
            <el-table-column prop="hidden" label="可见" show-overflow-tooltip>
              <template slot-scope="scope">
                <span v-if="scope.row.hidden">否</span>
                <span v-else>是</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建日期">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.gmtCreate) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="240">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click.native="handleDetail(scope.row)">详情</el-button>
                <el-button type="primary" size="mini" @click.native.stop="handleUpdate(scope.row)">编辑</el-button>
                <el-button type="primary" size="mini" @click.native.stop="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddMenuFormDialog
      :obj="obj"
      :dialog-visible="formDialogVisible"
      :dialog-type="dialogType"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getPage, del } from '@/api/system/menu'
import AddMenuFormDialog from './components/AddMenuFormDialog'
// import { download } from '@/api/data' downloadFile downloadUrl
import { parseTime, getDictLabel } from '@/utils/index'
export default {
  name: 'SystemMenuPage',
  components: {
    Pagination,
    AddMenuFormDialog
  },
  data() {
    return {
      obj: null,
      dialogType: 'add',
      assignBtnState: true,
      regBtnState: true,
      formDialogVisible: false,
      tableLoading: false,
      query: {
        page: 1, limit: 10, total: 0, blurry: ''
      },
      tableData: [],
      maps: new Map()
    }
  },
  created() {
  },
  mounted() {
    this.initData()
  },
  methods: {
    parseTime,
    getDictLabel,
    getMenus(tree, treeNode, resolve) {
      const params = { pid: tree.id }
      this.maps.set(tree.id, { tree, treeNode, resolve })
      setTimeout(() => {
        getPage(params).then(res => {
          resolve(res.data.list)
        })
      }, 100)
    },
    selectionChange(selection) {
      if (selection.length !== 1) {
        this.assignBtnState = true
        this.regBtnState = true
      } else {
        const row = this.$refs.table.selection[0]
        if (row.currentStatus !== 3) {
          this.regBtnState = false
        }
        if (row.assignStatus !== 1) {
          this.assignBtnState = false
        }
      }
    },
    handleUpdate(row) {
      this.obj = row
      this.dialogType = 'update'
      this.formDialogVisible = true
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        del([row.id]).then(result => {
          this.tableLoading = false
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleQuery(row.pid)
        }).catch(() => {
          this.tableLoading = false
        })
      }).catch(() => {
      })
    },
    initData(type, pid) {
      const query = JSON.parse(JSON.stringify(this.query))
      this.tableLoading = true
      getPage(query).then(result => {
        this.tableLoading = false
        this.query.total = result.data.total
        if (type === 'flush' && pid) {
          const { tree, treeNode, resolve } = this.maps.get(pid) // 根据pid取出对应的节点数据
          this.$set(this.$refs.table.store.states.lazyTreeNodeMap, pid, []) // 将对应节点下的数据清空，从而实现数据的重新加载
          this.getMenus(tree, treeNode, resolve)
          // this.$refs.table.store.states.treeData = result.data
          // this.$set(this.$refs.table.store.states.treeData, null, result.content) // 将对应节点下的数据清空，从而实现数据的重新加载
        } else {
          this.tableData = []
          if (result.data.list && result.data.list.length > 0) {
            this.$nextTick(() => {
              this.tableData = result.data.list
            })
          }
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleDetail(row) {
      this.obj = row
      this.dialogType = 'detail'
      this.formDialogVisible = true
    },
    handleQuery(pid) {
      this.query.page = 1
      this.initData('flush', pid)
    },
    handleAdd() {
      this.obj = null
      this.dialogType = 'add'
      this.formDialogVisible = true
    },
    clearQuery() {
      this.query = {
        page: 1, limit: 10, total: 0, blurry: '', pid: null
      }
    },
    flushPage() {
      // this.clearQuery()
      this.initData()
    },
    closeDialog() {
      this.formDialogVisible = false
    },
    resetQuery() {
      this.query = {
        ...this.query,
        blurry: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
